﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Net;
using System.Net.Mail;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using CMSControls;

namespace CMS
{
    public static class DisableControls
    {
     
        public static bool HasError { get; set; }

        public static void DisableControl(Control control)
        {
            bool Enable = false;
            var enablableUserControl = control as IEnablableUserControl;

            if (enablableUserControl != null)
            {
                enablableUserControl.Enabled = Enable;
                return;
            }

            if (control.Controls != null && control.Controls.Count > 0)
            {
                foreach (object item in control.Controls)
                {
                    var childControl = item as Control;

                    if (item != null)
                    {
                        DisableControl(childControl);
                    }
                }

                return;
            }

            var textbox = control as TextBox;

            if (textbox != null && textbox.TextMode == TextBoxMode.MultiLine)
            {
                textbox.ReadOnly = true;
                textbox.Enabled = false;

                return;
            }


            var webControl = control as WebControl;

            if (webControl != null && webControl.GetType() != typeof(Button) && webControl.AccessKey != "X")
            {
                webControl.Enabled = Enable;
                return;
            }

            var listControl = control as ListControl;

            if (listControl != null)
            {
                listControl.Enabled = Enable;
                return;
            }
        }
    }
}